package com.gdt.bdMessageQueue.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gdt.bdMessageQueue.advisory.Advisory;
import com.gdt.bdMessageQueue.pojo.ActiveMqMonitor;
import com.gdt.bdMessageQueue.service.ActiveMqMonitorService;
import com.gdt.bdMessageQueue.service.Server;



/**
 * 
 * @author zst
 * @description 精准位置服务平台
 */
@Controller
@RequestMapping("/bdService")
public class ActivemqController {
	
	@Autowired
	Server server;
	
	@Autowired
	Advisory advisory;
	
	@Autowired
	ActiveMqMonitorService acMService;
	
	
	/**
	 * 动态添加队列名字,开启activeMQ的消费者服务
	 * @param queueName
	 * @return 1表示启动成功,0表示启动失败
	 */
	private Map<String,Server> map = new HashMap<String, Server>();
	@ResponseBody
	@RequestMapping("startPreciseLocation")
	public String consumerStartService(@RequestParam String queueName,@RequestParam String startTime,@RequestParam String stopTime){
		//String status = acMService.getStatus(queueName);
		
		if(!(map.containsKey(queueName))){
			
			if(acMService.getActiveMqMonitorByQueueName(queueName)== null){
				acMService.insertValue(queueName,startTime,stopTime);
			}else{
				acMService.updateServiceTime(queueName, startTime, stopTime);
			}
			
			try {
				advisory.startAdvisory(queueName);
			} catch (Exception e) {
				e.printStackTrace();
			}
			server.startConnect(queueName);
			map.put(queueName, server);
		}else{
			return "服务已经开启,无需重复再启动";
		}
		return "开启服务端成功";
		
	}
	
	/**
	 * 关闭已经开启的activeMQ消费者服务
	 * @param queueName
	 * @return
	 */
	@ResponseBody
	@RequestMapping("stopPreciseLocation")
	public String consumerStopService(@RequestParam String queueName){
		
			if(map.containsKey(queueName)){
				Server server = map.get(queueName);
				try {
					server.stopSession();
					map.remove(queueName);
					return "消费服务已停止";
				} catch (Exception e) {
					e.printStackTrace();
					return "服务停止出现异常";
				}
			
			
		}else{
			return "需要停止的服务不存在";
		}
	}
	
	/**
	 * 查询平台已经开启的服务
	 * 直接查询数据库
	 */
	@ResponseBody
	@RequestMapping("searchAllQueueName")
	public List<String> queryAll(){
		List<String> lists = new ArrayList<String>();
		
		for(int i=0;i<map.size();i++){
			//map.
		}
		return lists;
	}
	
	/**
	 * 查询平台已经开启的服务
	 * 直接查询数据库
	 */
	/**
	 * 查询平台已经开启的服务
	 * 直接查询数据库
	 */
	@ResponseBody
	@RequestMapping("getName")
	public ActiveMqMonitor getActiveMqMonitorByQueueName(@RequestParam String queueName){
		
		return acMService.getActiveMqMonitorByQueueName(queueName);
	}
	
}
